// pages/eventtest/eventtest.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    leftOffset:0,
    topOffset:0,
    currentX:0,
    currentY:0,
    speedX:0,
    speedY:0,
    timer:null,
    lastMoveTime:0,


    mWorker:null,

    show:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.getNetworkType({
      success(res){
        console.log(res)
      }
    })
    wx.getLocalIPAddress({
      success(res) {
        console.log(res)
      }
    })

    let windowInfo = wx.getWindowInfo()
    console.log(windowInfo)


    wx.getSystemInfo({
      success(res) {
        console.log(res)
      }
    })

    let deviceInfo = wx.getDeviceInfo()
    console.log(deviceInfo)
  },

  touchStart(ev){
    this.setData({
      currentX:ev.changedTouches[0].clientX,
      currentY:ev.changedTouches[0].clientY,
    })
  },

  touchMove(ev){    
    this.setData({
      speedX:ev.changedTouches[0].clientX - this.data.currentX,
      speedY:ev.changedTouches[0].clientY - this.data.currentY,
      leftOffset:this.data.leftOffset + this.data.speedX,
      topOffset:this.data.topOffset + this.data.speedY,
      currentX:ev.changedTouches[0].clientX,
      currentY:ev.changedTouches[0].clientY,
      lastMoveTime:Date.now()
    })
  },

  touchEnd(ev){
    this.startScroll()
  },

  /**
   * 开始惯性滚动
   */
  startScroll(){
    var that = this
    var step = Date.now() - this.data.lastMoveTime
    console.log("startScroll:", step)
    var tempTimer = setInterval(
      function(){
        that.setData({
          speedX:that.data.speedX * 0.95,//减速，每次变为原来的0.95
          speedY:that.data.speedY * 0.95,
          leftOffset:that.data.leftOffset + that.data.speedX,
          topOffset:that.data.topOffset + that.data.speedY,
        })
        if(Math.abs(that.data.speedX)<=0.05&&Math.abs(that.data.speedY)<=0.05){
          clearInterval(that.data.timer); 
        }
      },10
    )

    this.setData({
      timer: tempTimer
    })

  },


  startWorder() {
    const worker = wx.createWorker('/worders/demo_worker.js')
    this.setData({
      mWorker:worker
    })
    worker.postMessage({
      msg: 'hello worker'
    })
    worker.onMessage(
      function(data) {
        console.log("msg  from worder:", data)
      }
    )
  },

  stopWorker(){
    this.data.mWorker.terminate()
  },

  //参考链接：https://developers.weixin.qq.com/miniprogram/dev/api/ui/interaction/wx.showToast.html
  showToast(event) {
    wx.showToast({
      title: 'title',
      icon:'none',
      duration:2000
    })
  },

  //参考链接：https://developers.weixin.qq.com/miniprogram/dev/api/ui/interaction/wx.showModal.html
  showDialog(event) {
    wx.showModal({
      title: 'title',
      content: 'content',
      // showCancel:false,
      complete: (res) => {
        if (res.cancel) {
          
        }
    
        if (res.confirm) {
          
        }
      }
    })
  },

  showLoadding(event) {
    wx.showLoading({
      title: 'title',
    })
    setTimeout(() => {
      wx.hideLoading()
    }, 2000);
  },

  showActionSheet(event) {
    wx.showActionSheet({
      alertText:"aletText",
      itemList: ['A', 'B', 'C'],
      success:(reslt) =>{//选中某一项时才会触发
        console.log("select:", reslt)
      }
    })
  },

  showCustomPop(){
    
    console.log("hide pop...")
    this.animate(
      "#custom_pop_bg",
      [
        { opacity: 0},
        { opacity: 0.5},
        { opacity: 1.0},
        ], 200, function () {
          console.log("anim end...")
          this.clearAnimation('#custom_pop_bg', { opacity: true }, function () {
            console.log("清除了#container上的opacity和rotate属性")
          })
      }.bind(this)
    )
    this.setData({
      show:true
    })
  },

  hideCustomPop() {
    console.log("hide pop...")
    this.animate(
      "#custom_pop_bg",
      [
        { opacity: 1.0},
        { opacity: 0.5},
        { opacity: 0.0},
        ], 200, function () {
          console.log("anim end...")
          this.setData({
            show:false
          })
          this.clearAnimation('#custom_pop_bg', { opacity: true }, function () {
            console.log("清除了#container上的opacity和rotate属性")
          })
      }.bind(this)
    )
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})